﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EmployeeApp.DAL.Entities;
using EmployeeApp.DAL.Models.Position;
using NHibernate;
using NHibernate.Criterion;

namespace EmployeeApp.DAL.Dal
{
   public class PositionDAL
    {
       public ISession Session;
       public PositionDAL(ISession session)
        {
            Session = session; 
        }

       public List<Position> GetAll(SearchCriteria searchCriteria)
        {
            var criteria = Session.CreateCriteria(typeof(Position));

            if (searchCriteria != null)
            {
                // Search
                if (!string.IsNullOrEmpty(searchCriteria.Code))
                {
                    criteria.Add(Restrictions.Like("Code", searchCriteria.Code, MatchMode.Start));
                }
                if (!string.IsNullOrEmpty(searchCriteria.Name))
                {
                    criteria.Add(Restrictions.Like("Name", searchCriteria.Name, MatchMode.Start));
                }

                //Sort
                if (!string.IsNullOrEmpty(searchCriteria.Sort))
                {
                    if (!string.IsNullOrEmpty(searchCriteria.SortType) && searchCriteria.SortType.ToLower() == "desc")
                    {
                        criteria.AddOrder(Order.Desc(searchCriteria.Sort));
                    }
                    else
                    {
                        criteria.AddOrder(Order.Asc(searchCriteria.Sort));
                    }

                }
            }

            return criteria.List<Position>().ToList();
        }
    }
}
